package edu.bit.water.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import edu.bit.water.entity.MetaEvaWuseInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import edu.bit.water.entity.MetaEvaWbalTest;
import edu.bit.water.service.MetaEvaWbalTestAssociationService;
import edu.bit.water.service.MetaEvaWbalTestService;

@Controller
@RequestMapping("/meta_eva_wbal_test")
public class MetaEvaWbalTestController {

	//记得加上这个哇，这是告诉全世界，你要开始在这类里面使用日志
	 protected static final Logger logger = LoggerFactory.getLogger(MetaEvaWbalTestController.class);
	 
	@Autowired
	private MetaEvaWbalTestService metaEvaWbalTestService;
	@Autowired
	private MetaEvaWbalTestAssociationService metaEvaWbalTestAssociationService;
	/**
	 ** 查询元数据表
	 **
	 * @return Map<String, Object> 如果查询成功则success为true,并返回metaSensorList
	 *         如果失败则返回false,errMsg中为结果
	 */
	@ResponseBody
	@RequestMapping(value = "/select", method = RequestMethod.GET)
	public Map<String, Object> queryMetaEvaWbalTest() {
		Map<String, Object> modelMap = new HashMap<>();
		List<MetaEvaWbalTest> metaEvaWbalTestList = new ArrayList<MetaEvaWbalTest>();
		try {
			metaEvaWbalTestList = metaEvaWbalTestService.queryMetaEvaWbalTest();
//			ObjectMapper mapper = new ObjectMapper();
//			String jsonlist = mapper.writeValueAsString(metaEvaWbalTestList); 
//			logger.info("");
//			logger.info("controll结果json:"+jsonlist);
			modelMap.put("status", "success");
			modelMap.put("rows", metaEvaWbalTestList);
		} catch (Exception e) {
			modelMap.put("status", "failed");
			modelMap.put("msg", e.getMessage());
		}
		return modelMap;
	}
	/**
	 * 查詢組號里列數
	 * @param faLevel 組號
	 * @return 查詢操作後信息
	 */
	@ResponseBody
	@RequestMapping(value = "/selectColumnNumByFaLevel", method = RequestMethod.GET)
	public Map<String, Object> queryColumnNumByFaLevel(Integer faLevel) {
		Map<String, Object> modelMap = new HashMap<>();
		try {
			int num = metaEvaWbalTestService.queryColumnNumByFaLevel((int)faLevel);
			modelMap.put("status", "success");
			modelMap.put("data", num);
		} catch (Exception e) {
			modelMap.put("status", "failed");
			modelMap.put("msg", e.getMessage());
		}
		return modelMap;
	}
	
	/**
	 * 
	 * @param metaMetaEvaWbalTest 元数据水平衡评估表实体类
	 * @return 成功返回1，失败返回0
	 */
	@ResponseBody
	@RequestMapping("/insert")
	public Map<String, Object> insertMetaEvaWbalTest(MetaEvaWbalTest metaEvaWbalTest) {
		MetaEvaWbalTest temp = metaEvaWbalTest;
		return metaEvaWbalTestService.insertMetaEvaWbalTest(temp);
	}
	/**
	 * 
	 * 
	 * @param metaSensor 元数据水平衡评估表实体类
	 * @return 成功返回1，失败返回0
	 * 改进：可以将关联表的数据删除放入service层
	 */
	@ResponseBody
	@RequestMapping("/delete")
	public Map<String, Object> deleteMetaEvaWbalTest(@RequestBody List<String> enNameList) {
		int flag = metaEvaWbalTestService.deleteMetaEvaWbalTest(enNameList);
		Map<String, Object> modelMap = new HashMap<>();
		if (flag == 1) {
			MetaEvaWbalTest metaEvaWbalTest = new MetaEvaWbalTest();
			for (String enName : enNameList) {
				metaEvaWbalTest.setEnName(enName);
				metaEvaWbalTestAssociationService.deleteMetaEvaWbalTestAssociation(metaEvaWbalTest);
			}
			modelMap.put("status", "success");
		}else {
			modelMap.put("status", "failed");
		}
		return modelMap;
	}

	@ResponseBody
	@RequestMapping("/update")
	public Map<String, Object> updateMetaEvaWbalTest(MetaEvaWbalTest metaEvaWbalTest) {
		MetaEvaWbalTest temp = metaEvaWbalTest;
		return metaEvaWbalTestService.updateMetaEvaWbalTest(temp);
	}
}